package com.jeeplus.modules.tms.util;


import com.jeeplus.modules.infor.entity.TakeAddress;
import com.jeeplus.modules.tms.entity.TmsWaybill;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import org.apache.pdfbox.multipdf.PDFMergerUtility;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by DHW on   12/13.
 */
public class PrintWayBill {

    /**
     *拣货单模版
     * @param tmsWaybill       实体（获取实体的信息填入模版）
     * @param templateFile  模版文件目录
     * @param file          要输出的文件
     * @throws Exception
     */
    public void updatePdfa(TmsWaybill tmsWaybill, TakeAddress t, List<Map<String,Object>> mapList, String templateFile, File file) throws Exception {

        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        PdfReader reader = new PdfReader(templateFile); // 模版文件目录
        FileOutputStream fos = new FileOutputStream(file);

        PdfStamper ps = new PdfStamper(reader, fos); // 生成的输出流

        BaseFont bf = BaseFont.createFont("c:/WINDOWS/fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);

        AcroFields s = ps.getAcroFields();

        //主表
        s.setFieldProperty("hetongbianhao", "textfont", bf, null);
        s.setFieldProperty("zuchehao", "textfont", bf, null);
        s.setFieldProperty("tuoyunlianxiren", "textfont", bf, null);
        s.setFieldProperty("tuoyunlianxidianhua", "textfont", bf, null);
        s.setFieldProperty("tuoyundizhi", "textfont", bf, null);
        s.setFieldProperty("chengyunren", "textfont", bf, null);
        s.setFieldProperty("chengunlianxiren", "textfont", bf, null);
        s.setFieldProperty("chengyunlianxidianhua", "textfont", bf, null);
        s.setFieldProperty("chengyundizhi", "textfont", bf, null);
        s.setFieldProperty("chengyunchepai", "textfont", bf, null);
        s.setFieldProperty("xingshizhenghao", "textfont", bf, null);
        s.setFieldProperty("cheliangdanwei", "textfont", bf, null);
        s.setFieldProperty("siji1", "textfont", bf, null);
        s.setFieldProperty("sijilianxi1", "textfont", bf, null);
        s.setFieldProperty("shenfenzheng1", "textfont", bf, null);
        s.setFieldProperty("jiashizheng1", "textfont", bf, null);
        s.setFieldProperty("siji2", "textfont", bf, null);
        s.setFieldProperty("sijilianxi2", "textfont", bf, null);
        s.setFieldProperty("shenfenzheng2", "textfont", bf, null);
        s.setFieldProperty("jiashizheng2", "textfont", bf, null);
        s.setFieldProperty("shouhuo", "textfont", bf, null);
        s.setFieldProperty("shouhuolianxi", "textfont", bf, null);
        s.setFieldProperty("shouhuodizhi", "textfont", bf, null);
        s.setFieldProperty("qiyun", "textfont", bf, null);
        s.setFieldProperty("daoda", "textfont", bf, null);
        s.setFieldProperty("qiyunnian", "textfont", bf, null);
        s.setFieldProperty("yundaonian", "textfont", bf, null);
        s.setFieldProperty("huodaowan", "textfont", bf, null);
        s.setFieldProperty("huodaoqian", "textfont", bf, null);
        s.setFieldProperty("huodaobai", "textfont", bf, null);
        s.setFieldProperty("huodaoshi", "textfont", bf, null);
        s.setFieldProperty("huodaoge", "textfont", bf, null);
        s.setFieldProperty("huodaoxiao", "textfont", bf, null);
        s.setFieldProperty("zhuanghuowan", "textfont", bf, null);
        s.setFieldProperty("zhuanghuoqian", "textfont", bf, null);
        s.setFieldProperty("zhuanghuobai", "textfont", bf, null);
        s.setFieldProperty("zhuanghuoshi", "textfont", bf, null);
        s.setFieldProperty("zhuanghuoge", "textfont", bf, null);
        s.setFieldProperty("zhuanghuoxiao", "textfont", bf, null);
        s.setFieldProperty("huizhiwan", "textfont", bf, null);
        s.setFieldProperty("huizhiqian", "textfont", bf, null);
        s.setFieldProperty("huizhibai", "textfont", bf, null);
        s.setFieldProperty("huizhige", "textfont", bf, null);
        s.setFieldProperty("huizhixiao", "textfont", bf, null);
        s.setFieldProperty("hejiwan", "textfont", bf, null);
        s.setFieldProperty("hejiqian", "textfont", bf, null);
        s.setFieldProperty("hejibai", "textfont", bf, null);
        s.setFieldProperty("hejige", "textfont", bf, null);
        s.setFieldProperty("hejixiao", "textfont", bf, null);
        s.setFieldProperty("hejishi", "textfont", bf, null);
        s.setFieldProperty("shoukuanren", "textfont", bf, null);
        s.setFieldProperty("kaihuhang", "textfont", bf, null);
        s.setFieldProperty("jiayoukahao", "textfont", bf, null);
        s.setFieldProperty("kahao", "textfont", bf, null);
        s.setFieldProperty("jiayoukajine", "textfont", bf, null);
        s.setFieldProperty("tuoyunrenqian", "textfont", bf, null);
        s.setFieldProperty("xianchang", "textfont", bf, null);
        s.setFieldProperty("chengyunqian", "textfont", bf, null);
        s.setFieldProperty("qiandingnian1", "textfont", bf, null);
        s.setFieldProperty("qiandingyue1", "textfont", bf, null);
        s.setFieldProperty("qiandingri1", "textfont", bf, null);
        s.setFieldProperty("qiandingnian2", "textfont", bf, null);
        s.setFieldProperty("qiandingri2", "textfont", bf, null);
        s.setFieldProperty("qiandingyue2", "textfont", bf, null);
        s.setFieldProperty("qiyunyue", "textfont", bf, null);
        s.setFieldProperty("qiyunri", "textfont", bf, null);
        s.setFieldProperty("yundaoyue", "textfont", bf, null);
        s.setFieldProperty("yundaori", "textfont", bf, null);

        //子表
        s.setFieldProperty("pinming1", "textfont", bf, null);
        s.setFieldProperty("pinming2", "textfont", bf, null);
        s.setFieldProperty("pinming3", "textfont", bf, null);
        s.setFieldProperty("guige1", "textfont", bf, null);
        s.setFieldProperty("guige2", "textfont", bf, null);
        s.setFieldProperty("guige3", "textfont", bf, null);
        s.setFieldProperty("baozhuang1", "textfont", bf, null);
        s.setFieldProperty("baozhuang2", "textfont", bf, null);
        s.setFieldProperty("baozhuang3", "textfont", bf, null);
        s.setFieldProperty("shuliang1", "textfont", bf, null);
        s.setFieldProperty("shuliang2", "textfont", bf, null);
        s.setFieldProperty("shuliang3", "textfont", bf, null);
        s.setFieldProperty("zhongliang1", "textfont", bf, null);
        s.setFieldProperty("zhongliang2", "textfont", bf, null);
        s.setFieldProperty("zhongliang3", "textfont", bf, null);
        s.setFieldProperty("tiji1", "textfont", bf, null);
        s.setFieldProperty("tiji2", "textfont", bf, null);
        s.setFieldProperty("tiji3", "textfont", bf, null);
        s.setFieldProperty("huizhihao", "textfont", bf, null);
        s.setFieldProperty("beizhu", "textfont", bf, null);

        //设置值（主表）
        s.setField("hetongbianhao", tmsWaybill.getCode());
        s.setField("zuchehao", tmsWaybill.getForwardingPrice());
        s.setField("tuoyunlianxiren", t.getPerson());
        s.setField("tuoyunlianxidianhua", t.getPhone());
        s.setField("tuoyundizhi", t.getAddress());
        s.setField("chengyunren", tmsWaybill.getCommonCarrier());
        s.setField("chengyunlianxiren", tmsWaybill.getContacts());
        s.setField("chengyunlianxidianhua", "");
        s.setField("chengyundizhi", "");
        s.setField("chengyunchepai", tmsWaybill.getPlateNumber());
        s.setField("xingshizhenghao", tmsWaybill.getDrivingLicense());
        s.setField("cheliangdanwei", "");
        s.setField("siji1", tmsWaybill.getDriverName());
        s.setField("sijilianxi1", tmsWaybill.getDriverPhone());
        s.setField("shenfenzheng1", tmsWaybill.getDriveNumber());
        s.setField("jiashizheng1", tmsWaybill.getDriveNumber());
        s.setField("sijilianxi2", "");
        s.setField("shenfenzheng2", "");
        s.setField("jiashizheng2", "");
        s.setField("shouhuo",tmsWaybill.getUnitName() );
        s.setField("shouhuolianxi",tmsWaybill.getUnitConfirmor()+","+ tmsWaybill.getPhone());
        s.setField("shouhuodizhi", tmsWaybill.getSite());
        s.setField("qiyun", tmsWaybill.getContractDistance());
        s.setField("daoda", tmsWaybill.getArrive());
        Date date =new Date();
        SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd");
       String d= myFmt2.format(date);
        s.setField("qiyunnian", d.substring(0,4));
        s.setField("yundaonian", "");
        int w;
        int q;
        int b;
        int sh;
        int g;
        int a;
        if(tmsWaybill.getKdPayment()!=null&&tmsWaybill.getKdPayment().length()>0){
             a=Integer.parseInt(tmsWaybill.getKdPayment());
             w=a/10000;
             q=a%10000/1000;
            b=a%1000/100;
             sh=a%100/10;
            g=a%10;
            s.setField("huodaowan", getNumber(w));
            s.setField("huodaoqian", getNumber(q));
            s.setField("huodaobai", getNumber(b));
            s.setField("huodaoshi", getNumber(sh));
            s.setField("huodaoge", getNumber(g));
            s.setField("huodaoxiao", tmsWaybill.getKdPayment());
        }
        if(tmsWaybill.getLoadingPayment()!=null&&tmsWaybill.getLoadingPayment().length()>0){
            a=Integer.parseInt(tmsWaybill.getLoadingPayment());
            w=a/10000;
            q=a%10000/1000;
            b=a%1000/100;
            sh=a%100/10;
            g=a%10;
            s.setField("zhuanghuowan", getNumber(w));
            s.setField("zhuanghuoqian", getNumber(q));
            s.setField("zhuanghuobai", getNumber(b));
            s.setField("zhuanghuoshi", getNumber(sh));
            s.setField("zhuanghuoge", getNumber(g));
            s.setField("zhuanghuoxiao", tmsWaybill.getLoadingPayment());
        }
        if(tmsWaybill.getReceiptCalculate()!=null&&tmsWaybill.getReceiptCalculate().length()>0){
            a=Integer.parseInt(tmsWaybill.getReceiptCalculate());
            w=a/10000;
            q=a%10000/1000;
            b=a%1000/100;
            sh=a%100/10;
            g=a%10;
            s.setField("huizhiwan", getNumber(w));
            s.setField("huizhiqian", getNumber(q));
            s.setField("huizhibai", getNumber(b));
            s.setField("huizhishi", getNumber(sh));
            s.setField("huizhige", getNumber(g));
            s.setField("huizhixiao", tmsWaybill.getReceiptCalculate());
        }
        if(tmsWaybill.getTotalCost()!=null&&tmsWaybill.getTotalCost().length()>0){
            a=Integer.parseInt(tmsWaybill.getTotalCost());
            w=a/10000;
            q=a%10000/1000;
            b=a%1000/100;
            sh=a%100/10;
            g=a%10;
            s.setField("hejiwan", getNumber(w));
            s.setField("hejiqian", getNumber(q));
            s.setField("hejibai", getNumber(b));
            s.setField("hejishi", getNumber(sh));
            s.setField("hejige", getNumber(g));

            s.setField("hejixiao", tmsWaybill.getTotalCost());
        }



        s.setField("shoukuanren", tmsWaybill.getPayeeName());
        s.setField("kaihuhang", tmsWaybill.getBankAddress());
        s.setField("jiayoukahao", tmsWaybill.getLoadingCardNumber());
        s.setField("kahao", tmsWaybill.getCardNumber());
        s.setField("jiayoukajine", tmsWaybill.getLoadingJe());
        s.setField("tuoyunrenqian", "");
        s.setField("xianchang", "");
        s.setField("chengyunqian", "");
        s.setField("qiandingnian1", "");
        s.setField("qiandingyue1", "");
        s.setField("qiandingri1", "");
        s.setField("qiandingnian2", "");
        s.setField("qiandingri2", "");
        s.setField("qiandingyue2", "");
        s.setField("qiyunyue", d.substring(5,7));
        s.setField("qiyunri", d.substring(8,10));
        s.setField("yundaoyue", "");
        s.setField("yundaori", "");
        //子表
        NumberFormat nf = NumberFormat.getInstance();
        for (int i =0; i< mapList.size(); i++) {
            int j=i+1;
            s.setField("pinming"+j, (String) mapList.get(i).get("type"));
            s.setField("guige"+j, "");
            s.setField("baozhuang"+j, "");
            s.setField("shuliang"+j, mapList.get(i).get("gross")+"");
            s.setField("zhongliang"+j,  nf.format(mapList.get(i).get("weight")));
            s.setField("tiji"+j,  nf.format(mapList.get(i).get("volume")));
        }
        s.setField("huizhihao", tmsWaybill.getShipmentNumbers());
        s.setField("beizhu", "");

        ps.setFormFlattening(true); // 这句不能少

        ps.close();
        reader.close();
        fos.close();
    }


    public  static  String getNumber(int n){
        if(n==0){return "零";}
        else if(n==1){return "壹";}
        else if(n==2){return "贰";}
        else if(n==3){return "叁";}
        else if(n==4){return "肆";}
        else if(n==5){return "伍";}
        else if(n==6){return "陆";}
        else if(n==7){return "柒";}
        else if(n==8){return "捌";}
        else {return "玖";}
    }
    /**
     *
     * @param folder       要合并文件的文件夹
     * @param outPdfFile    合并后的文件名
     * @param fileString    需要合并的pdf文件名集合
     * @throws Exception
     */
    public void mergePdf(String folder, String outPdfFile, List<String> fileString) throws Exception {
        PDFMergerUtility mergePdf = new PDFMergerUtility();
        List<String> filesInFolder = fileString;
        for (int i = 0; i < filesInFolder.size(); i++)
            mergePdf.addSource(folder + File.separator + filesInFolder.get(i));
        mergePdf.setDestinationFileName(outPdfFile);
        mergePdf.mergeDocuments();
    }

    public void updatePdfa1(TmsWaybill tmsWaybill,String ck, String templateFile, File file) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(templateFile); // 模版文件目录
        FileOutputStream fos = new FileOutputStream(file);

        PdfStamper ps = new PdfStamper(reader, fos); // 生成的输出流

        BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
                BaseFont.NOT_EMBEDDED);

        AcroFields s = ps.getAcroFields();

        //主表
        s.setFieldProperty("Text1", "textfont", bf, null);
        s.setFieldProperty("Text2", "textfont", bf, null);
        s.setFieldProperty("Text3", "textfont", bf, null);
        s.setFieldProperty("Text4", "textfont", bf, null);
        s.setFieldProperty("Text5", "textfont", bf, null);
        s.setFieldProperty("Text6", "textfont", bf, null);
        s.setFieldProperty("Text7", "textfont", bf, null);
        s.setFieldProperty("Text8", "textfont", bf, null);
        s.setFieldProperty("Text9", "textfont", bf, null);
        s.setFieldProperty("Text10", "textfont", bf, null);
        s.setFieldProperty("Text11", "textfont", bf, null);

        s.setField("Text1", tmsWaybill.getForwardingPrice());
        s.setField("Text2", tmsWaybill.getDriverName());
        s.setField("Text3", tmsWaybill.getPlateNumber());
        s.setField("Text4", tmsWaybill.getRentingCost());
        int a=Integer.parseInt(tmsWaybill.getRentingCost());
        int w=a/10000;
        int q=a%10000/1000;
        int b=a%1000/100;
        int  sh=a%100/10;
        int g=a%10;
        s.setField("Text5", getNumber(w));
        s.setField("Text6", getNumber(q));
        s.setField("Text7", getNumber(g));
        s.setField("Text8", getNumber(sh));
        s.setField("Text9", getNumber(b));
        s.setField("Text10", tmsWaybill.getContractDistance());
        s.setField("Text11", tmsWaybill.getArrive());
        ps.setFormFlattening(true); // 这句不能少

        ps.close();
        reader.close();
        fos.close();
    }
}
